﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HA.ModFlow.Infrastructure
{

    /// <summary>
    /// 用于存储三维下的网格数据,
    /// </summary>
    [Serializable]
   public class Grid3dValue
    {


        #region 构造函数

        /// <summary>
        /// 
        /// </summary>
        /// <param name="i">行数（Rows外围边框上点）</param>
        /// <param name="j">列数</param>
        /// <param name="k">层数</param>
        public Grid3dValue(int i, int j, int k)
        {
            this._i = i;
            this._j = j;
            this._k = k;

            _datas = new double[i, j, k];

        }


        #endregion

        double[, ,] _datas;

        public double[, ,] Datas
        {
            get { return _datas; }

        }



        int _i;

        /// <summary>
        /// 行数
        /// </summary>
        public int I
        {
            get { return _i; }

        }

        int _j;

        /// <summary>
        /// 列数
        /// </summary>
        public int J
        {
            get { return _j; }

        }

        int _k;

        /// <summary>
        /// 层数
        /// </summary>
        public int K
        {
            get { return _k; }

        }


        double _inEffectiveMinValue = -888.0;
        /// <summary>
        /// 无效值
        /// </summary>
        public double InEffectiveMinValue
        {
            get { return _inEffectiveMinValue; }
            set { _inEffectiveMinValue = value; }
        }






        #region 共有方法

        /// <summary>
        /// 取得 结果数据 Grid3dValue （三维数组）中的最大最小值
        /// </summary>
        /// <returns></returns>
        public List<double> GetGrid3DValueMinMaxScalar()
        {
            try
            {
                double minScalar = this.Datas[0, 0, 0];
                double maxScalar = this.Datas[0, 0, 0];

                List<double> scalarMinMax = new List<double>();

                for (int layer = 0; layer < this.K; layer++)
                {
                    for (int row = 0; row < this.I; row++)
                    {
                        for (int col = 0; col < this.J; col++)
                        {
                            if (this.Datas[row, col, layer] < minScalar)
                            {
                                if (Math.Abs(this.Datas[row, col, layer] - this._inEffectiveMinValue) < 1e-6)//比较两个double值是否相等 很不安全 ！！
                                //if (this.Datas[row, col, layer] == this._inEffectiveValue)
                                {

                                }
                                else
                                {
                                    minScalar = this.Datas[row, col, layer];
                                }

                            }
                            if (this.Datas[row, col, layer] > maxScalar)
                            {
                                maxScalar = this.Datas[row, col, layer];
                            }
                        }
                    }
                }


                scalarMinMax.Add(minScalar);
                scalarMinMax.Add(maxScalar);

                return scalarMinMax;

            }
            catch (Exception)
            {
                return null;
            }

        }


        /// <summary>
        /// 
        /// 取得数据并以指定的存放形式返回  可以返回指定层和所有层的数据
        /// 
        /// </summary>
        /// <param name="indexLayer">指定层号</param>
        /// <param name="layerCellScalar">存储返回的指定层的数据</param>
        /// <param name="gridCellScalar">存储 所有网格的数据</param>
        /// <returns></returns>
        public bool GetGridData(int indexLayer, double[,] layerCellScalar, List<double[,]> gridCellScalar)
        {
            try
            {
                if (indexLayer < 0 || indexLayer > K - 1)
                {
                    return false;
                }

                if (layerCellScalar == null)
                {
                    layerCellScalar = new double[this.I, this.J];

                    //return false;

                }
                else
                {

                    if (layerCellScalar.GetLength(0) != this.I || layerCellScalar.GetLength(1) != this.J)
                    {
                        return false;
                    }

                }
                if (gridCellScalar == null)
                {

                    gridCellScalar = new List<double[,]>();

                    //return false;
                }
                else
                {
                    gridCellScalar.Clear();
                }

                for (int layer = 0; layer < this.K; layer++)
                {
                    double[,] layerScalar = new double[this.I, this.J];

                    for (int row = 0; row < this.I; row++)
                    {
                        for (int col = 0; col < this.J; col++)
                        {


                            if (layer == indexLayer)
                            {
                                layerCellScalar[row, col] = this.Datas[row, col, layer];

                                layerScalar[row, col] = this.Datas[row, col, layer];


                            }
                            else
                            {

                                layerScalar[row, col] = this.Datas[row, col, layer];

                            }
                        }
                    }

                    gridCellScalar.Add(layerScalar);


                }


                return true;
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("从运行结果产生的数据中提取CellScalar值时出错！ 哈哈 -_-  请联系小李！");
                return false;
            }



        }
        /// <summary>
        /// 取得数据并以指定的存放形式返回  可以返回指定层和所有层的数据
        /// </summary>
        /// <param name="indexLayer">指定层号</param>
        /// <param name="layerCellScalar">存储返回的指定层的数据</param>
        /// <returns></returns>
        public bool GetLayerGridData(int indexLayer, double[,] layerCellScalar)
        {


            try
            {
                if (layerCellScalar == null)
                {

                    layerCellScalar = new double[this.I, this.J];

                    //return false;

                }
                else
                {

                    if (layerCellScalar.GetLength(0) != this.I || layerCellScalar.GetLength(1) != this.J)
                    {
                        return false;
                    }

                }

                for (int layer = 0; layer < this.K; layer++)
                {

                    if (layer == indexLayer)
                    {

                        for (int row = 0; row < this.I; row++)
                        {
                            for (int col = 0; col < this.J; col++)
                            {

                                layerCellScalar[row, col] = this.Datas[row, col, layer];

                            }
                        }

                    }
                    else
                    {
                        continue;

                    }

                }


                return true;

            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("从运行结果产生的数据中提取CellScalar值时出错！ 哈哈 -_-  请联系小李！");
                return false;
            }


        }

        /// <summary>
        /// 取得数据并以指定的存放形式返回  可以返回指定层和所有层的数据
        /// </summary>
        /// <param name="gridCellScalar">存储 所有网格的数据</param>
        /// <returns></returns>
        public bool GetAllGridData(List<double[,]> gridCellScalar)
        {

            try
            {
                if (gridCellScalar == null)
                {
                    gridCellScalar = new List<double[,]>();

                    //return false;
                }
                else
                {
                    gridCellScalar.Clear();
                }

                for (int layer = 0; layer < this.K; layer++)
                {
                    double[,] layerScalar = new double[this.I, this.J];

                    for (int row = 0; row < this.I; row++)
                    {
                        for (int col = 0; col < this.J; col++)
                        {

                            layerScalar[row, col] = this.Datas[row, col, layer];

                        }
                    }

                    gridCellScalar.Add(layerScalar);

                }


                return true;
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("从运行结果产生的数据中提取CellScalar值时出错！ 哈哈 -_-  请联系小李！");
                return false;
            }

        }



        #endregion


    }
}
